<?php

namespace common\models;

use feehi\cdn\QiniuTarget;
use Yii;
use yii\helpers\Url;
use common\helpers\Util;

/**
 * This is the model class for table "photo".
 *
 * @property int     $id id
 * @property int     $uid 用户id
 * @property string  $brife 简介
 * @property string  $img 图片地址
 * @property string  $iso ISO
 * @property int     $status 状态
 * @property string  $type 类型
 * @property string  $make 品牌
 * @property string  $lens 镜头
 * @property string  $model 器材/型号
 * @property string  $exposer 曝光
 * @property string  $fnumber 光圈
 * @property double  $flength 焦距
 * @property string  $created_at 上传时间
 * @property string  $updated_at 修改时间
 * @property integer $author_id 作者
 */
class Photo extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'photo';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['uid'], 'required'],
            [['id', 'uid', 'status'], 'integer'],
            [['flength'], 'number'],
            [['created_at', 'updated_at'], 'safe'],
            [['brife', 'img', 'iso', 'type', 'make', 'lens', 'model', 'exposer', 'fnumber'], 'string', 'max' => 255],
            [['id'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'         => 'id',
            'uid'        => '用户id',
            'brife'      => '简介',
            'img'        => '图片地址',
            'iso'        => 'ISO',
            'status'     => '状态',
            'type'       => '类型',
            'make'       => '品牌',
            'lens'       => '镜头',
            'model'      => '器材/型号',
            'exposer'    => '曝光',
            'fnumber'    => '光圈',
            'flength'    => '焦距',
            'created_at' => '上传时间',
            'updated_at' => '修改时间',
        ];
    }


    public function beforeSave($insert)
    {

        if (parent::beforeSave($insert)) {

            if ($insert) {
                $this->status     = 1; //设置默认状态为正常
                $this->created_at = date('Y-m-d H:i:s');
                $this->updated_at = date('Y-m-d H:i:s');


                //分析处理exif

            } else {
                $this->updated_at = date('Y-m-d H:i:s');
            }

            return true;

        } else {
            return false;
        }

    }

    public function getShortBrife()
    {
        $tmpStr = strip_tags($this->brife);
        $tmpLen = mb_strlen($tmpStr);

        return mb_substr($tmpStr, 0, 20, 'utf-8') . (($tmpLen > 20) ? '...' : '');
    }

    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'uid']);
    }

    public function getLikes()
    {
        return 1;
    }

    public function getStatus0()
    {
        return $this->hasOne(Photostatus::className(), ['id' => 'status']);
    }

    public function getImgUrl()
    {
//        return Url::to("$this->img", true);
        //TODO:上传七牛云
        /** @var QiniuTarget $cdn */
        $cdn = Yii::$app->get("cdn");
        if ($cdn->exists($this->img)) {
           return $cdn->getCdnUrl($this->img);
        } else {
            $locfile = Yii::getAlias("@photo") . '/' . $this->img;
//            var_dump($locfile);
            if (file_exists(Yii::getAlias("@photo") . '/' . $this->img)) {
                $cdn->upload($locfile, $this->img);
            }
            return "http://img.hipicture.top/" . $this->img;
        }


    }

    public function getIsLiked()
    {
        $uid = User::findIdentityByAccessToken(Yii::$app->request->getHeaders()->get('access-token'))->id;
        if (Like::findOne(['uid' => $uid, 'pid' => $this->id]) != null) {
            return true;
        }

        return false;
    }

    public function getIsCollected()
    {
        $uid = $uid = User::findIdentityByAccessToken(Yii::$app->request->getHeaders()->get('access-token'))->id;
        if (\api\models\Collect::findOne(['uid' => $uid, 'pid' => $this->id]) != null) {
            return true;
        }

        return false;
    }

}
